﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;

namespace APMDemo
{
    /// <summary>
    /// 异步编程Task,async/await demo
    /// </summary>
    class TaskDemo
    {
        //static void Main(string[] args)
        //{
        //    Console.WriteLine("Call Start");
        //    Task<string> t1 = DoAsync("AAAA");
        //    Console.WriteLine("DoAsync AAAA");

        //    Task<string> t2 = DoSomthingAsync("BBBB",5);
        //    Console.WriteLine("DoAsync BBBB");

        //    Console.WriteLine(t1.Result);
        //    Console.WriteLine(t2.Result);
        //    Console.WriteLine("Call End");

        //    Console.ReadLine();
        //}

        static async Task<string> DoAsync(string str)
        {
            Console.WriteLine("-------DoAsync 开始-------");
            // 遇到await，立即返回到（调用方法）19行继续向下执行
            await Task.Delay(TimeSpan.FromSeconds(3));
            // 3秒后继续向下执行
            string r = await DoSomthingAsync(str,2);
            Console.WriteLine("-------DoAsync 结束-------");
            return $"DoAsync : {r}";
        }

        static async Task<string> DoSomthingAsync(string str,int secondes)
        {
            Console.WriteLine($"-------DoSomthingAsync {str} 开始-------");
            // 遇到await，立即返回调用方法继续执行
            await Task.Delay(TimeSpan.FromSeconds(secondes));
            Console.WriteLine($"-------DoSomthingAsync {str} 结束-------");
            return $"do somthing...{str}";
        }

        /**

        Call Start
        -------DoAsync 开始-------
        DoAsync AAAA
        -------DoSomthingAsync BBBB 开始-------
        DoAsync BBBB
        -------DoSomthingAsync AAAA 开始-------
        -------DoSomthingAsync AAAA 结束-------
        -------DoAsync 结束-------
        DoAsync : do somthing...AAAA
        -------DoSomthingAsync BBBB 结束-------
        do somthing...BBBB
        Call End

        **/
    }
}
